from django.http import HttpResponse, JsonResponse
from django.views import View

from apps.users.models import User

# 1. 定义检测用户名是否重复的类试图
class UsernameCountView(View):
    def get(self, request, username):
        # return HttpResponse(username)
        # 1. 去数据库中查 username 等于 username 的用户的个数
        count = User.objects.filter(username=username).count()
        # 2. 返回格式{"code": 0, "errmsg": "ok", "count": 1}的数据
        return JsonResponse({'code':0, 'errmsg': 'ok', 'count': count})